package com.example.za.account.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.example.za.account.R;
import com.example.za.account.adapter.PagerAdapter;
import com.example.za.account.utils.DateUtils;
import com.example.za.account.utils.ScreenUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ReportFragment extends BaseFragment {

    @BindView(R.id.chart_tab_strip)
    PagerSlidingTabStrip chartTabStrip;
    @BindView(R.id.pager_chart)
    ViewPager pagerChart;
    Unbinder unbinder;
    private View rootView;
    private PieIncomeFragment pieIncomeFragment;
    private PieExpenseFragment pieExpenseFragment;
    private PagerAdapter pagerAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = super.onCreateView(inflater, container, savedInstanceState);
            unbinder = ButterKnife.bind(this, rootView);
            List<Fragment> fragments = new ArrayList<>(2);
            pieIncomeFragment = new PieIncomeFragment();
            pieExpenseFragment = new PieExpenseFragment();
            fragments.add(pieIncomeFragment);
            fragments.add(pieExpenseFragment);
            pagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager(), fragments);
            pagerChart.setAdapter(pagerAdapter);
            chartTabStrip.setViewPager(pagerChart);
            chartTabStrip.setTextSize(ScreenUtils.dp2sp(getActivity(), 16));
        }

        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshChart(String message) {
        if (TextUtils.equals(message, "income_inserted") || TextUtils.equals(message, "income_updated")
                || TextUtils.equals(message, "income_deleted") || TextUtils.equals(message, "invest_inserted")) {
            pieIncomeFragment.updateData(DateUtils.getMonthStart(), DateUtils.getMonthEnd());
            pagerAdapter.setRefresh(new boolean[]{true, false});
        }
        if (TextUtils.equals(message, "expense_inserted") || TextUtils.equals(message, "expense_updated")
                || TextUtils.equals(message, "expense_deleted")) {
            pieExpenseFragment.updateData(DateUtils.getMonthStart(), DateUtils.getMonthEnd());
            pagerAdapter.setRefresh(new boolean[]{false, true});
        }
        pagerAdapter.notifyDataSetChanged();

    }

    @Override
    protected Fragment getSubFragment() {
        return this;
    }

    @Override
    protected int getResId() {
        return R.layout.fragment_report;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

}
